package com.cqc.listviewdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView listView;
    private List<String> itemList = new ArrayList<>();
    private View layout_empty_view;
    private ArrayAdapter<String> adapter;
    private Button btn1;
    private Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initData();
        findViews();
        initViews();
    }

    private void initViews() {
        adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, itemList);
        listView.setAdapter(adapter);
        listView.setEmptyView(layout_empty_view);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        layout_empty_view.setOnClickListener(this);
    }

    private void findViews() {
        listView = (ListView) findViewById(R.id.listView);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        layout_empty_view = findViewById(R.id.layout_empty_view);
    }

    private void initData() {
        for (int i = 0; i < 50; i++) {
            itemList.add("张" + i);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                itemList.clear();
                for (int i = 0; i < 100; i++) {
                    itemList.add("李" + i);
                }
                listView.setAdapter(adapter);//可以
//              adapter.notifyDataSetChanged();//无效，因为listView.setAdapter(null);
                break;
            case R.id.btn2:
                listView.setAdapter(null);
                break;
            case R.id.layout_empty_view:
                itemList.clear();
                for (int i = 0; i < 100; i++) {
                    itemList.add("李" + i);
                }
                listView.setAdapter(adapter);//可以
//              adapter.notifyDataSetChanged();//无效，因为listView.setAdapter(null);
                break;
        }
    }
}
